package com.zeus.base.smscenter.service.impl;

import com.zeus.base.privilege.utils.ResultModel;
import com.zeus.base.smscenter.mapper.TenantMapper;
import com.zeus.base.smscenter.model.SmsSupplier;
import com.zeus.base.smscenter.model.SmsSupplierProperty;
import com.zeus.base.smscenter.model.Tenant;
import com.zeus.base.smscenter.service.ISmsSettingService;
import com.zeus.base.smscenter.service.ISupplierPropertyService;
import com.zeus.base.smscenter.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 短信项目设定Service
 */
@Service
@Transactional(readOnly = true)
public class SmsSettingServiceImpl implements ISmsSettingService {
    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private ISupplierService iSupplierService;

    @Autowired
    private ISupplierPropertyService iSupplierPropertyService;

    @Override
    @Transactional(readOnly = true)
    public List<Tenant> findList(Tenant tenant) {
        List<Tenant> list = tenantMapper.findList(tenant);
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public Tenant selectByCode(String code) {

        return tenantMapper.selectByCode(code);
    }

    @Override
    @Transactional(readOnly = true)
    public Tenant findByCode(Tenant tenant) {

        return tenantMapper.findByCode(tenant);
    }

    @Override
    @Transactional(readOnly = true)
    public Tenant selectByPrimaryKey(Integer id) {

        return tenantMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Tenant selectByPrimaryKey(String id) {
        return tenantMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Tenant get(Tenant entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(Tenant entity) {
        ResultModel result = exists(entity);
        if (result.isSuccess()) {
            int r = tenantMapper.insert(entity);
            if (r == 0)
                result = new ResultModel(false, "操作保存失败");
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insertSelective(Tenant entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(Tenant entity) {
        ResultModel result = exists(entity);
        if (result.isSuccess()) {
            int r = tenantMapper.updateByPrimaryKey(entity);
            if (r == 0)
                result = new ResultModel(false, "操作保存失败");
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(Tenant entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Integer id) {
        int r = tenantMapper.deleteByPrimaryKey(id);
        if (r == 0)
            return new ResultModel(false, "操作保存失败");
        return new ResultModel(true, "操作保存成功");
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Tenant entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel exists(Tenant entity) {
        List<Tenant> list = null;//tenantMapper.exists(entity);
        if (list == null || list.size() == 0)
            return new ResultModel(true, "操作保存成功");
        for (Tenant t : list) {
//            if (t.getConsumeTypeCode().toLowerCase().equals(entity.smsSettingMapper().toLowerCase())) {
//                return new ResultModel(false, "消费项目编码已经存在");
//            }
            return new ResultModel(false, "消费项目名称已经存在");
        }
        return new ResultModel(true, "操作保存成功");
    }


    @Override
    @Transactional(readOnly = false)
    public ResultModel deleteByCode(String code) {
        int r = tenantMapper.deleteByCode(code);
        if (r == 0)
            return new ResultModel(false, "操作删除失败");
        return new ResultModel(true, "操作删除成功");
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(String[] keyIds) {
        List<String[]> failedIds = new ArrayList<String[]>();
        SmsSupplierProperty smsSupplierProperty = new SmsSupplierProperty();
        SmsSupplierProperty smsSupplierPropertyCond = new SmsSupplierProperty();
        SmsSupplier smsSupplier = new SmsSupplier();
        for (String code : keyIds) {
            smsSupplierProperty.setTenantCode(code);
            //删除前,先删除该租户下的所有供应商及供应商属性后,再删除租户信息
            //删除供应商属性
            smsSupplierPropertyCond.setTenantCode(code);
            iSupplierPropertyService.deleteByCode(smsSupplierPropertyCond);
            //删除供应商
            smsSupplier.setTenantCode(code);
            iSupplierService.deleteByTenantCode(smsSupplier);
            //删除租户
            int r = tenantMapper.deleteByCode(code);
            if (r==0) {
                failedIds.add(new String[]{code, "数据删除失败!"});
            }
        }
        return new ResultModel(true, 0, "", failedIds);
    }

    /**
     * 查询租户下的所有短信账号
     * @param tenant
     * @return
     */
    @Override
    public List<Map<String,Object>> findByHeadCode(Tenant tenant) {
        return tenantMapper.findByHeadCode(tenant);
    }
}
